package cn.xinyangtong.shopping.rest.service;

import cn.xinyangtong.common.util.ConstantsUtil;
import cn.xinyangtong.facade.rest.ContentService;
import cn.xinyangtong.mapper.TbContentMapper;
import cn.xinyangtong.pojo.TbContent;
import cn.xinyangtong.pojo.TbContentExample;
import cn.xinyangtong.shopping.rest.dao.JedisClient;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fengbo on 2017/7/19.
 */
public class ContentServiceImpl implements ContentService {

    private final Logger log = LoggerFactory.getLogger(ContentServiceImpl.class);

    @Autowired
    private TbContentMapper contentMapper;
    @Autowired
    private JedisClient jedisClient;

    @Override
    public String getContentList(Long contentCid) {
        //从缓存中取内容
        try {
            String result = jedisClient.hget(ConstantsUtil.INDEX_CONTENT_REDIS_KEY, contentCid + "");
            if (!StringUtils.isEmpty(result)) {
                return result;
            }
        } catch (Exception e) {
            log.error("Select ContentList From Redis Fails,[ContentCid] = " + contentCid);
            e.printStackTrace();
        }

        //根据内容分类id查询内容列表
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(contentCid);
        //执行查询
        List<TbContent> list = contentMapper.selectByExample(example);
        List<Map> resultList = new ArrayList<>();
        //创建一个jsp页码要求的pojo列表
        for (TbContent tbContent : list) {
            Map map = new HashMap<>();
            map.put("src", tbContent.getPic());
            map.put("height", 399);
            map.put("width", 670);
            map.put("srcB", tbContent.getPic2());
            map.put("widthB", 550);
            map.put("heightB", 399);
            map.put("href", tbContent.getUrl());
            map.put("alt", tbContent.getSubTitle());
            resultList.add(map);
        }
        String result = JSON.toJSONString(resultList);

        try {
            jedisClient.hset(ConstantsUtil.INDEX_CONTENT_REDIS_KEY, contentCid + "", result);
        } catch (Exception e) {
            log.error("Insert ContentList To Redis Fails, [ContentCid] = " + contentCid);
            e.printStackTrace();
        }

        return result;
    }
    //前台接受的数据
//    var data = [
//    {
//        "srcB":"/images/5653e38eNed7f4ffc.jpg",
//            "height":399,
//            "alt":"",
//            "width":670,
//            "src":"/images/5653e38eNed7f4ffc.jpg",
//            "widthB":550,
//            "href":"javascript:;",
//            "heightB":399
//    },
//    {
//        "srcB":"/images/5656b3deNcc43202a.jpg",
//            "height":399,
//            "alt":"",
//            "width":670,
//            "src":"/images/5656b3deNcc43202a.jpg",
//            "widthB":550,
//            "href":"javascript:;",
//            "heightB":399
//    },
//    {
//        "srcB":"/images/56569f87N3b9ee5a6.jpg",
//            "height":399,
//            "alt":"",
//            "width":670,
//            "src":"/images/56569f87N3b9ee5a6.jpg",
//            "widthB":550,
//            "href":"javascript:;",
//            "heightB":399
//    },
//    {
//        "srcB":"/images/565c0755N3d995d38.jpg",
//            "height":399,
//            "alt":"",
//            "width":670,
//            "src":"/images/565c0755N3d995d38.jpg",
//            "widthB":550,
//            "href":"javascript:;",
//            "heightB":399
//    },
//    {
//        "srcB":"/images/565bfb0cNaf023e06.jpg",
//            "height":399,
//            "alt":"",
//            "width":670,
//            "src":"/images/565bfb0cNaf023e06.jpg",
//            "widthB":550,
//            "href":"javascript:;",
//            "heightB":399
//    },
//    {
//        "srcB":"/images/565c189bNf9721325.jpg",
//            "height":399,
//            "alt":"",
//            "width":670,
//            "src":"/images/565c189bNf9721325.jpg",
//            "widthB":550,
//            "href":"javascript:;",
//            "heightB":399
//    }
//    		];
}
